#include "commanddefine.h"

namespace CommandDefine {

    // ========== 灯光控制指令 ==========
    // const QByteArray CK_APPENDIX_LASER_ON   = "AA AB VOA_ON 00");
    const QByteArray CK_APPENDIX_LASER_ON   = "AA AB VOA_ON 00";
    const QByteArray CK_APPENDIX_LASER_OFF  = "AA AB VOA_OFF 00";
    const QByteArray CK_APPENDIX_RED_ON     = "AA AB RED_ON 00";
    const QByteArray CK_APPENDIX_RED_OFF    = "AA AB RED_OFF 00";
    const QByteArray CK_APPENDIX_GREEN_ON   = "AA AB GREEN_ON 00";
    const QByteArray CK_APPENDIX_GREEN_OFF  = "AA AB GREEN_OFF 00";
    const QByteArray CK_APPENDIX_BLUE_ON    = "AA AB BLUE_ON 00";
    const QByteArray CK_APPENDIX_BLUE_OFF   = "AA AB BLUE_OFF 00";

    // ========== 衰减器控制指令 ==========
    const QByteArray CK_VOA_ON                  = "AA AB VOA AB FF";
    const QByteArray CK_VOA_OFF                 = "AA AB VOA AB 00";
    const QByteArray CK_VOA_SET_VOLTAGE_TEMPLATE = "AA AB VOA AB VOL XXXX FF";
    // const QByteArray CKR_VOA_REPLY              = "AAABVOAABVOLXXXXOKFF";

    // =========== 光纤炉温度指令 ==========
    const QByteArray CK_FURNACE_QUERY           = "AA AB AA BB FF";
    const QByteArray CKR_FURNACE_REPLY          = "AA AB AA XX XX BB YY YY FF";

    // ========== 温、湿度、气压传感器 (ID:5) ==========
    const QByteArray CK_ENV_QUERY               = "AA AB ZENSOR FF";
    const QByteArray CKR_ENV_REPLY              = "AA AB ZS XXXXYYYYZZZZ FF";

    // ========== 光开关指令 (OS) ==========
    const QByteArray CK_OPTICAL_SWITCH_TEMPLATE = "AA AB OS 0X FF";
    const QByteArray CKR_OPTICAL_SWITCH_OK      = "AA AB OS 0X ON FF";

    // ========== 调焦指令 (LMT) ==========
    const QByteArray CK_FOCUS_AB = "AA AB LMT AB FF";
    const QByteArray CK_FOCUS_AC = "AA AB LMT AC FF";
    const QByteArray CK_FOCUS_AD = "AA AB LMT AD FF";
    const QByteArray CK_FOCUS_BC = "AA AB LMT BC FF";
    const QByteArray CK_FOCUS_BD = "AA AB LMT BD FF";
    const QByteArray CK_FOCUS_CD = "AA AB LMT CD FF";

    // ========== 回零/位置控制 (FUS ID:1) ==========
    const QByteArray CK_FUS_HOME                = "AA AB FUS ZEM FF";
    const QByteArray CK_FUS_POS                 = "AA AB FUS PSM FF";
    const QByteArray CK_FUS_STOP                = "AA AB FUS STOP FF";
    const QByteArray CK_FUS_SET_SPEED_POS_TEMPLATE = "AA AB FUS SET xxxxxxxxxx yyyyyyyyyy FF";
    const QByteArray CKR_FUS_READY              = "AA AB FUS RDY FF";
    const QByteArray CKR_FUS_SET_OK             = "AA AB FUS SET OK FF";
    const QByteArray CKR_FUS_WARN               = "AA AB FUS WARN FF";

    // ========== 风扇控制器 (FAN ID:4) ==========
    const QByteArray CK_FAN_SET_TEMP_TEMPLATE   = "AA AB FAN SET XXXYYY FF";
    const QByteArray CK_FAN_READ_TEMP           = "AA AB FAN RD FF";
    const QByteArray CKR_FAN_SET_OK             = "AA AB FAN SET OK FF";
    const QByteArray CKR_FAN_REPLY_TEMP         = "AA AB FAN RD XXX FF";

    // ========== 激光器温度控制 (LAS ID:3) ==========
    const QByteArray CK_LAS_SET_TEMP_TEMPLATE   = "AA AB LAS SET XXXXXXXXX FF";
    const QByteArray CK_LAS_QUERY_TEMP          = "AA AB LAS RD FF";
    const QByteArray CKR_LAS_SET_OK             = "AA AB LAS SET OK FF";
    const QByteArray CKR_LAS_REPLY_TEMP         = "AA AB LAS RD XXXXXXXXX FF";

    // ========== 光纤炉温度控制(大) (FTC ID:2) ==========
    const QByteArray CK_FTC_SET_TEMP_TEMPLATE   = "AA AB FTC SET K XXXXXXXXX FF";
    const QByteArray CK_FTC_QUERY_TEMP          = "AA AB FTC RD K FF";
    const QByteArray CKR_FTC_SET_OK             = "AA AB FTC SET OK FF";
    const QByteArray CKR_FTC_SET_ERR            = "AA AB FTC SET ERR FF";
    const QByteArray CKR_FTC_REPLY_TEMP         = "AA AB FTC RD K XXXXXXXXX FF";
    const QByteArray CKR_FTC_REPLY_ERR          = "AA AB FTC RD ERR FF";


    // ========== 函数（指令构建） ==========

    QByteArray buildVOASetVoltage(int voltage)
    {
        if ((voltage < 0) || (voltage > 5000))
            return {};
        QString hex = QString("%1").arg(voltage, 4, 10, QChar('0')); // 10进制4位
        QByteArray cmd = CK_VOA_SET_VOLTAGE_TEMPLATE;
        cmd.replace("XXXX", hex.toLatin1());
        return cmd;
    }

    QByteArray buildFANSetTemp(int startTemp, int fullTemp)
    {
        if ((startTemp < 0) || (startTemp > 255) || (fullTemp < 0) || (fullTemp > 255))
            return {};
        QString startHex = QString("%1").arg(startTemp, 3, 10, QChar('0'));
        QString fullHex = QString("%1").arg(fullTemp, 3, 10, QChar('0'));
        QByteArray cmd = CK_FAN_SET_TEMP_TEMPLATE;
        cmd.replace("XXX", startHex.toLatin1());
        cmd.replace("YYY", fullHex.toLatin1());
        return cmd;
    }

    QByteArray buildFUSSetSpeedPos(double speed, double pos)
    {
        qint32 s = static_cast<qint32>(speed); // 不保留小数
        qint32 p = static_cast<qint32>(pos);
        // qint32 s = static_cast<qint32>(speed * 100000); // 保留5位小数
        // qint32 p = static_cast<qint32>(pos * 100000);
        QString sHex = QString("%1").arg(s, 10, 10, QChar('0'));
        QString pHex = QString("%1").arg(p, 10, 10, QChar('0'));
        // qDebug() << sHex << "---" << pHex;

        QByteArray cmd = CK_FUS_SET_SPEED_POS_TEMPLATE;
        cmd.replace("xxxxxxxxxx", sHex.toLatin1());
        cmd.replace("yyyyyyyyyy", pHex.toLatin1());
        return cmd;
    }

    QByteArray buildLASSetTemp(double temp)
    {
        if ((temp < -400.0) || (temp > 1000.0))
            return {};
        qint32 raw = static_cast<qint32>(temp * 100000);
        QString num = QString("%1").arg(raw, 9, 10, QChar('0')); // 10进制9位
        QByteArray cmd = CK_LAS_SET_TEMP_TEMPLATE;
        cmd.replace("XXXXXXXXX", num.toLatin1());
        return cmd;
    }

    QByteArray buildFTCSetTemp(int channel, double temp)
    {
        if ((channel != 1) && (channel != 2))
            return {};
        if ((temp < -400.0) || (temp > 1000.0))
            return {};
        qint32 raw = static_cast<qint32>(temp * 100000);
        QString num = QString("%1").arg(raw, 9, 10, QChar('0'));
        QByteArray cmd = CK_FTC_SET_TEMP_TEMPLATE;
        cmd.replace("K", QString::number(channel).toLatin1());
        cmd.replace("XXXXXXXXX", num.toLatin1());
        return cmd;
    }

    QByteArray buildFTCQueryTemp(int channel)
    {
        if ((channel != 1) && (channel != 2))
            return {};
        QByteArray cmd = CK_FTC_QUERY_TEMP;
        cmd.replace("K", QString::number(channel).toLatin1());
        return cmd;
    }

    QByteArray buildOpticalSwitch(int channel)
    {
        if ((channel < 0) || (channel > 4))
            return {};
        QByteArray cmd = CK_OPTICAL_SWITCH_TEMPLATE;
        cmd.replace("X", QString::number(channel).toLatin1());
        return cmd;
    }

}
